#include "mainwindow.h"
#include <QtGui>
#include <QHBoxLayout>
#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    QWidget *widget = new QWidget(this);
    setCentralWidget(widget);

    media = new Phonon::MediaObject();
    videoWidget = new Phonon::VideoWidget();
    videoWidget->resize(600,450);
    videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    Phonon::createPath(media, videoWidget);

    audio = new Phonon::AudioOutput();
    Phonon::createPath(media, audio);

    volumeSlider = new Phonon::VolumeSlider();
    volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    volumeSlider->setAudioOutput(audio);

    seekSlider = new Phonon::SeekSlider();
    seekSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    seekSlider->setMediaObject(media);

    volumeLabel = new QLabel();
    volumeLabel->setText("Volume:");
    volumeLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    addFileButton = new QPushButton();
    addFileButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(addFileButton, SIGNAL(released()), this, SLOT(addFiles()));

    addDirButton = new QPushButton();
    addDirButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(addDirButton, SIGNAL(released()), this, SLOT(addDir()));

    clearListButton = new QPushButton();
    clearListButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    playButton = new QPushButton();
    playButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(playButton, SIGNAL(released()), this, SLOT(playVideo()));

    nextButton = new QPushButton();
    nextButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(nextButton, SIGNAL(released()), this, SLOT(playNext()));

    lastButton = new QPushButton();
    lastButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(lastButton, SIGNAL(released()), this, SLOT(playLast()));

    fileList = new QListWidget();
    fileList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);


    playButton->setText(">");
    nextButton->setText(">>");
    lastButton->setText("<<");
    addFileButton->setText("open...");

    QHBoxLayout *hVolumeLayout = new QHBoxLayout();
    hVolumeLayout->addWidget(volumeLabel);
    hVolumeLayout->addWidget(volumeSlider);
    //hVolumeLayout->sizeHint();

    QHBoxLayout *hPlayButtonLayout = new QHBoxLayout();
    hPlayButtonLayout->addWidget(lastButton);
    hPlayButtonLayout->addWidget(playButton);
    hPlayButtonLayout->addWidget(nextButton);
    hPlayButtonLayout->addWidget(addFileButton);

    QHBoxLayout *hPlayAndVolumeLayout = new QHBoxLayout();
    hPlayAndVolumeLayout->addLayout(hPlayButtonLayout);
    printFileName = new QLabel();
    hPlayAndVolumeLayout->addWidget(printFileName);
    hPlayAndVolumeLayout->addLayout(hVolumeLayout);

    QVBoxLayout *vVideoPlayLayout = new QVBoxLayout();
    vVideoPlayLayout->addWidget(videoWidget);
    vVideoPlayLayout->addWidget(seekSlider);
    vVideoPlayLayout->addLayout(hPlayAndVolumeLayout);

    QHBoxLayout *hMainLayout = new QHBoxLayout(widget);
    hMainLayout->addLayout(vVideoPlayLayout);
    hMainLayout->addWidget(fileList);

    connect(fileList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(playByDoubleClick()));


    files = new QStringList();
}

MainWindow::~MainWindow()
{

}

void MainWindow::addFiles()
{
    QStringList tempList = QFileDialog::getOpenFileNames(this, "select one or more to add to playlist",QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
    *files << tempList;
    const QStringList printList = *files;
   // fileList->destroyed();
    fileList->addItems(printList);
}

void MainWindow::addDir()
{

}

void MainWindow::playVideo()
{
    //currentPlay = new QString("E:\\baidu player\\1942.rmvb");
    currentPlay = 0;
    if(!files->isEmpty())
    {
        media->setCurrentSource(Phonon::MediaSource(files->at(currentPlay)));
        //const QString tmp = files->at(currentPlay);
        //printFileName->setText(tmp);
        //videoWidget->resize(600, 450);
        media->play();
    }
}

void MainWindow::playByDoubleClick()
{
    media->setCurrentSource(Phonon::MediaSource(fileList->selectedItems().at(0)->text()));
    //const QString tmp = fileList->selectedItems().at(0)->text().indexOf(".", 0);
    //printFileName->setText(tmp);
    //videoWidget->resize(600, 450);
    media->play();
}

void MainWindow::playLast()
{

}

void MainWindow::playNext()
{

}
